package cn.org.eshow.service.impl;

import cn.org.eshow.bean.query.ProductQuery;
import cn.org.eshow.bean.query.PromotionQuery;
import cn.org.eshow.common.page.Page;
import cn.org.eshow.dao.ProductDao;
import cn.org.eshow.dao.PromotionDao;
import cn.org.eshow.model.Product;
import cn.org.eshow.model.Promotion;
import cn.org.eshow.service.PromotionManager;
import cn.org.eshow.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.jws.WebService;
import java.util.Date;
import java.util.List;

@WebService(serviceName = "PromotionService", endpointInterface = "cn.org.eshow.service.PromotionManager")
@Service
public class PromotionManagerImpl extends GenericManagerImpl<Promotion, Integer> implements PromotionManager {

    @Autowired
    PromotionDao promotionDao;
    @Autowired
    ProductDao productDao;

    public PromotionManagerImpl() {
    }

    @Autowired
    public PromotionManagerImpl(PromotionDao promotionDao) {
        super(promotionDao);
        this.promotionDao = promotionDao;
    }

    @Override
    public List<Promotion> list(PromotionQuery query) {
        return promotionDao.list(query);
    }

    @Override
    public Page<Promotion> search(PromotionQuery query) {
        return promotionDao.search(query);
    }

    @Override
    public Promotion check(PromotionQuery query) {
        List<Promotion> list = promotionDao.list(query);
        if (!list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

//    @Override
//    public Promotion save(Promotion promotion, String productIds, String productDiscounts) {
//        //促销开始结束时间设定为00开始与59结束
//        promotion.setStartTime(DateUtil.getStartOfDate(promotion.getStartTime()));
//        promotion.setEndTime(DateUtil.getEndOfDate(promotion.getEndTime()));
//
//        if (promotion.getStartTime().before(new Date()) && promotion.getEndTime().after(new Date())) {
//            promotion.setState(1);//进行中
//        }
//        if (promotion.getStartTime().after(new Date())) {
//            promotion.setState(0);//未开始
//        }
//        if (promotion.getEndTime().before(new Date())) {
//            promotion.setState(-1);//已过期
//        }
//        if (promotion.getLimitNumber() > 0) {
//            promotion.setIfLimit(Boolean.TRUE);
//        }
//        promotion = promotionDao.save(promotion);
//        String[] productIdArray = productIds.split(",");
//        String[] productDiscountArray = productDiscounts.split(",");
//        for (int i = 0; i < productIdArray.length; i++) {
//            Product product = productDao.get(Integer.valueOf(productIdArray[i]));
//            if (product != null) {
//                if (promotion.getStartTime().before(new Date()) && promotion.getEndTime().after(new Date())) {
//                    product.setIfDiscount(Boolean.TRUE);
//                } else {
//                    product.setIfDiscount(Boolean.FALSE);//暂未开始促销
//                }
//                product.setDiscount(Double.valueOf(productDiscountArray[i]));//商品实际折扣
////                product.setPromotionPrice(product.getPrice() * product.getDiscount());//商品促销价格
//                product.setPromotion(promotion);
//                productDao.save(product);
//            }
//        }
//        return promotion;
//    }

    @Override
    public void enabled(Promotion promotion) {
        List<Product> products = productDao.list(new ProductQuery(Boolean.TRUE, promotion.getId(), Boolean.TRUE));
        for (Product product : products) {
            product.setIfDiscount(Boolean.FALSE);
            productDao.save(product);
        }
        promotion.setEnabled(Boolean.FALSE);
        promotionDao.save(promotion);
    }

    @Override
    public long count(PromotionQuery query) {
        return promotionDao.count(query);
    }


}